package com.jgp.common.persistence;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jgp.common.utils.JGPUtil;
import io.swagger.annotations.ApiModel;
import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 项目   jgp
 * 作者   loufei
 * 时间   2017/8/14
 */
@ApiModel("模型基类")
@MappedSuperclass
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Model implements IdModel,Serializable {
    
    private static final long serialVersionUID = -6435041570618609104L;
    
    /**
     * 创建时间
     */
    @DateTimeFormat(pattern = JGPUtil.DATE_TIME_FORMATTER_PATTERN)
    @Column(name = "create_datetime")
    private LocalDateTime createDatetime;
    @Column(name = "create_ttp")
    private Long createTimestamp;
    /**
     * 修改时间
     */
    @DateTimeFormat(pattern = JGPUtil.DATE_TIME_FORMATTER_PATTERN)
    @Column(name = "modify_datetime", nullable = false)
    private LocalDateTime modifyDatetime;
    
    @Column(name = "modify_ttp")
    private Long modifyTimestamp;
    /**
     * 删除标志
     */
    @JsonIgnore
    @Type(type = "yes_no")
    @Column(name = "del_flag")
    private Boolean delFlag;

    @JsonIgnore
    @Column(name = "row_id", length = 32)
    private String rowId;
    
    @JsonIgnore
    @Column(name = "dep_id", length = 32)
    private String depId;
    
    @JsonIgnore
    @Column(name = "client_id", length = 32)
    private String clientId;
    
    @Transient
    private Map<String,Object> __EXT_DATA__;
    
    public Map<String, Object> get__EXT_DATA__() {
        return __EXT_DATA__;
    }
    
    public void set__EXT_DATA__(Map<String, Object> __EXT_DATA__) {
        this.__EXT_DATA__ = __EXT_DATA__;
    }
    
    protected Model() {
    
    }
    
    public LocalDateTime getCreateDatetime() {
        return createDatetime;
    }
    
    public void setCreateDatetime(LocalDateTime createDatetime) {
        this.createDatetime = createDatetime;
    }
    
    public LocalDateTime getModifyDatetime() {
        return modifyDatetime;
    }
    
    public void setModifyDatetime(LocalDateTime modifyDatetime) {
        this.modifyDatetime = modifyDatetime;
    }
    
    
    public Boolean getDelFlag() {
        return delFlag;
    }
    
    public void setDelFlag(Boolean delFlag) {
        this.delFlag = delFlag;
    }
    
    public String hash() {
        return super.toString();
    }
    
    
    @Override
    @Transient
    public abstract Object getId();
    
    @Transient
    public abstract void setId(Object id);
    
    public Long getCreateTimestamp() {
        return createTimestamp;
    }
    
    public void setCreateTimestamp(Long createTimestamp) {
        this.createTimestamp = createTimestamp;
    }
    
    public Long getModifyTimestamp() {
        return modifyTimestamp;
    }
    
    public void setModifyTimestamp(Long modifyTimestamp) {
        this.modifyTimestamp = modifyTimestamp;
    }
    
    public String getRowId() {
        return rowId;
    }
    
    public void setRowId(String rowId) {
        this.rowId = rowId;
    }
    
    public String getClientId() {
        return clientId;
    }
    
    public void setClientId(String clientId) {
        this.clientId = clientId;
    }
    
    public void addExtData(String name, Object value) {
        if(Objects.isNull(this.__EXT_DATA__)){
            this.__EXT_DATA__ = new HashMap<>();
        }
        this.__EXT_DATA__.put(name, value);
    }
    
    @JsonIgnore
    public Map<String, Object> getExtData() {
        return this.__EXT_DATA__;
    }
    
    public void setExtData(Map<String, Object> extData) {
        if(Objects.isNull(this.__EXT_DATA__)){
            this.__EXT_DATA__ = new HashMap<>();
        }
        this.__EXT_DATA__.clear();
        this.__EXT_DATA__.putAll(extData);
    }
}
